package com.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.common.pojo.TaotaoResult;
import com.mall.common.utils.IDUtils;
import com.mall.mapper.TbItemDescMapper;
import com.mall.mapper.TbItemMapper;
import com.mall.mapper.TbItemParamItemMapper;
import com.mall.pojo.*;
import com.mall.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbItemDescMapper tbItemDescMapper;

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Override
    public TbItem getItemById(long id) {

        //第一种查询方式
        //TbItem tbItem=tbItemMapper.selectByPrimaryKey(id);

        //第二种查询方式
        TbItemExample example=new TbItemExample();
        TbItemExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(id);

        List<TbItem> list= tbItemMapper.selectByExample(example);

        if(list!=null && list.size()>0){
            TbItem item=list.get(0);
            return item;
        }

        return null;
    }

    @Override
    public EUDataGridResult getItemList(int page, int rows) {

        TbItemExample example=new TbItemExample();

        PageHelper.startPage(page,rows);
        List<TbItem> list=tbItemMapper.selectByExample(example);

        PageInfo<TbItem> itemPageInfo=new PageInfo<>(list);
        long total= itemPageInfo.getTotal();

        EUDataGridResult euDataGridResult=new EUDataGridResult();
        euDataGridResult.setRows(list);
        euDataGridResult.setTotal(total);

        return euDataGridResult;
    }

    @Override
    public TaotaoResult createItem(TbItem tbItem,String desc,String itemParam) throws Exception {

        Long itemId= IDUtils.genItemId();
        tbItem.setId(itemId);

        tbItem.setStatus((byte) 1);
        tbItem.setCreated(new Date());
        tbItem.setUpdated(new Date());

        tbItemMapper.insert(tbItem);

        //添加商品描述信息
        TaotaoResult taotaoResult=insertItemDesc(itemId,desc);

        //添加商品规格参数
        TaotaoResult taotaoResult1=insertItemParam(itemId,itemParam);


        if(taotaoResult.getStatus()!=200 && taotaoResult1.getStatus()!=200){
            throw new Exception();
        }

        return TaotaoResult.ok();
    }


    private TaotaoResult insertItemDesc(Long itemId,String desc){

        TbItemDesc tbItemDesc=new TbItemDesc();

        tbItemDesc.setItemId(itemId);
        tbItemDesc.setCreated(new Date());
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setUpdated(new Date());

        tbItemDescMapper.insert(tbItemDesc);

        return TaotaoResult.ok();
    }


    //添加规格参数
    private TaotaoResult insertItemParam(Long itemId,String itemParam){

        TbItemParamItem itemParamItem=new TbItemParamItem();

        itemParamItem.setItemId(itemId);
        itemParamItem.setCreated(new Date());
        itemParamItem.setUpdated(new Date());
        itemParamItem.setParamData(itemParam);

        tbItemParamItemMapper.insert(itemParamItem);

        return TaotaoResult.ok();
    }

}
